/*
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.* ;

import org.apache.catalina.connector.Request;

@Controller
public class helloController {

    @RequestMapping("/0")
    @ResponseBody
    public String Say(){
        return "index";
    }


    @RequestMapping("/1")
    //@ResponseBody
    public ModelAndView mod(){

        System.out.println("help!!!!!!!!!!!!!!!!!!!!!");
        ModelAndView model = new ModelAndView();
        model.setViewName("abc");
        return model;
    }
}
*/
package com.example.demo.controller;
import java.sql.Date;
import java.util.List;
import java.util.Map;

import java.sql.*;
import java.util.ArrayList;
import com.example.demo.controller.Emp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@ComponentScan
@RestController
@RequestMapping("/Emp")
public class UsersController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * @return
     * 查询全部信息
     */
    @RequestMapping("/list")
    @ResponseBody
    public List<Map<String, Object>> EmpList() {
        System.out.println("已经入该函数");
        String sql = "select * from emp";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;

    }


        @RequestMapping("/empno")
        public @ResponseBody String  selEmp(Emp emp) {
            System.out.println("进入查询函数");
            String sql = "select * from emp where empno = ? " ;
            Object args[] = {emp.getEmpno()};
            int temp = jdbcTemplate.update(sql, args);
            if(temp > 0) {
                return "查找成功";
            }
            return "查找失败";
        }
    @RequestMapping("/1")
    //@ResponseBody
    public ModelAndView mod(){

        System.out.println("help!!!!!!!!!!!!!!!!!!!!!");
        ModelAndView model = new ModelAndView();
        model.setViewName("abc");
        return model;
    }
    /**
     * @return
     * 跳转到新增接界面
     */
    @RequestMapping("/to")
    public ModelAndView  toAdd() {
        ModelAndView mav = new ModelAndView("cc");
        return mav;
    }
    @RequestMapping("/do")
    public ModelAndView  doAdd() {
        ModelAndView mav = new ModelAndView("dd");
        return mav;
    }
    /**
     * 新增数据
     * @param emp
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody boolean addEmp(Emp emp) {

        String sql = "insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) value (?,?,?,?,?,?,?,?)";
        Object args[] = {emp.getEmpno(),emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return true;
        }
        return false;
    }
    @RequestMapping("/doadd")
    public @ResponseBody boolean addDept(Dept dept) {
        System.out.println("????");
        String sql = "insert into dept (deptno,dname,loc) value (?,?,?)";
        Object args[] = {dept.getDeptno(),dept.getDname(),dept.getLoc()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return true;
        }
        return false;
    }

    /**
     * @param emp
     * @return
     * 删除数据
     */
    @RequestMapping("/del")
    public @ResponseBody String  delEmp(Emp emp) {
        System.out.println("进入删除函数");
        String sql = " DELETE FROM emp WHERE empno = ?";
        Object args[] = {emp.getEmpno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return "文章删除成功";
        }
        return "删除出现错误";
    }

    @RequestMapping("/dodel")
    public @ResponseBody String  delDept(Dept dept) {
        System.out.println("进入删除函数");
        String sql = " DELETE FROM dept WHERE deptno = ?";
        Object args[] = {dept.getDeptno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return "文章删除成功";
        }
        return "删除出现错误";
    }
    /**
     * @param emp
     * @return
     * 更新操作
     */


    @RequestMapping("/upd")
    public @ResponseBody String  updEmp(Emp emp) {
        System.out.println("进入更新界面");
        String sql = "update emp set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? where empno = ?  ";    //set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=?
        Object args[] = {emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno(),emp.getEmpno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return "文章修改成功";
        }
        return "修改出现错误";
    }

    @RequestMapping("/doupd")
    public @ResponseBody String  updDept(Dept dept) {
        System.out.println("进入更新界面");
        String sql = "update dept set dname=?,loc=? where deptno = ?  ";    //set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=?
        Object args[] = {dept.getDname(),dept.getLoc(),dept.getDeptno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return "文章修改成功";
        }
        return "修改出现错误";
    }

}
